package com.sport.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class ConfirmDialog extends DialogFragment {

    public Activity mActivity;

    protected View rootView;

    private boolean canCancel = true;
    private TextView tvMessage;
    private TextView tvConfirmBtn;
    private TextView tvCancelBtn;
    private Listener mListener;

    public ConfirmDialog(Activity activity, String message) {

        Bundle bundle = new Bundle();
        bundle.putString("message", message);
        //传入值，跟Fragment传值方法一样
        this.setArguments(bundle);
        mActivity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AnimationAlphaDialog);
        if (!canCancel) {
            setCancelable(false);
        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.confirm_dialog, container);
        tvMessage = (TextView) rootView.findViewById(R.id.confirm_message);
        tvConfirmBtn = (TextView) rootView.findViewById(R.id.confirm_ok_btn);
        tvCancelBtn = (TextView) rootView.findViewById(R.id.confirm_cancel_btn);
        if (getArguments() != null) {
            tvMessage.setText(getArguments().getString("message"));
        }
        ViewTreeObserver observer = tvMessage.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                ViewTreeObserver obs = tvMessage.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);

                if (tvMessage.getLineCount() == 1) {
                    tvMessage.setGravity(Gravity.CENTER_HORIZONTAL);
                } else {
                    tvMessage.setGravity(Gravity.LEFT);
                }
            }
        });
        tvConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) mListener.onConfirm();
                dismiss();
            }
        });
        tvCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        return rootView;
    }

    @Override
    public void onStart() {
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = ScreenUtil.getScreenWidth(mActivity) / 3 * 2;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;

        getDialog().getWindow().setAttributes(layoutParams);
        super.onStart();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        if (!canCancel) {
            getDialog().setCancelable(false);
            getDialog().setCanceledOnTouchOutside(false);
        }

        super.onActivityCreated(savedInstanceState);
    }

    public void setOnclickLister(Listener listener) {
        mListener = listener;
    }

    public ConfirmDialog setCanCancel(boolean canCancel) {
        this.canCancel = canCancel;
        return this;
    }

    public interface Listener {
        public void onConfirm();
    }
}
